//
//  STCommentViewController.swift
//  Story
//
//  Created by 汤泽川 on 2019/4/25.
//  Copyright © 2019 汤泽川. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class STCommentViewController: UIViewController, STStoryCommentViewDelegate {

    private var commentInfo : STCommentInfo
    private let disposeBag = DisposeBag()
    public weak var delegate : STStoryCommentViewDelegate?
    init(commentModel : STCommentInfo) {
        self.commentInfo = commentModel
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        buildView()
    }
    
    private func buildView() {
        let closeBtn = UIButton()
        closeBtn.setImage(#imageLiteral(resourceName: "story_back"), for: .normal)
        self.view.addSubview(closeBtn)
        closeBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(self.view.safeAreaInsets.top + 42)
            make.left.equalToSuperview().offset(12)
            make.width.height.equalTo(22)
        }
        closeBtn.rx.controlEvent(.touchUpInside).subscribe(onNext: { [weak self] in
            self?.dismiss(animated: true, completion: nil)
        }).disposed(by: disposeBag)
        
        let titleLabel = UILabel()
        titleLabel.text = self.title
        titleLabel.textColor = UIColor.black
        titleLabel.font = .systemFont(ofSize: 17)
        self.view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalTo(closeBtn)
        }
        
        let line = UIView()
        line.backgroundColor = UIColor.gray
        self.view.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.top.equalTo(closeBtn.snp_bottom).offset(8)
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        
        let commentVM = STStoryCommentViewModel(commentId: self.commentInfo.commentInfo.id, storyId: nil,authorId:  self.commentInfo.userInfo.id)
        let commentView = STStoryCommentView(viewModel: commentVM)
        commentView.delegate = self
        self.view.addSubview(commentView)
        commentView.setupCommentHeader(model: self.commentInfo)
        commentVM.loadMore()
        commentView.snp.makeConstraints { (make) in
            make.bottom.left.right.equalToSuperview()
            make.top.equalTo(line.snp_bottom).offset(4)
        }
    }
    
    func didClickComment(model: STCommentInfo) {
        let commentVC = STCommentViewController(commentModel: model)
        commentVC.title = String(model.replayCount) + "条回复"
        self.present(commentVC, animated: true, completion: nil)
    }
    func didClickUserAvatar(authorName: String) {
        self.dismiss(animated: true) {
            self.delegate?.didClickUserAvatar(authorName: authorName)
        }
    }
    
}
